<?php
class Db_Chief
{
    public static function total($condition=false) {
        static $totals;
        $key = hash('crc32b', $condition);
        if(!isset($totals[$key]))
            $totals[$key] = DB::count('chief', $condition, __FILE__ . ' line ' . __LINE__);
        return $totals[$key];
    }

    public static function getChiefs($limit='', $condition='')
    {
        static $chiefs;
        $key = hash('crc32b', $condition.$limit);

        if(!isset($chiefs[$key]))
        {
            if($condition)
            {
            	$condition = 'WHERE '.$condition;
            }

            $chiefs[$key] =  DB::fetch_all("SELECT * FROM chief $condition $limit", __FILE__ . ' line ' . __LINE__);
        }

        return $chiefs[$key];
    }




    public static function getChiefsId($limit='', $condition='', $indexId = true){
        static $chiefs;
        $key = hash('crc32b', $condition.$limit);

        if(!isset($chiefs[$key]))
        {
            if($condition)
            {
                $condition = 'WHERE '.$condition;
            }
            if($indexId){
                $chiefs[$key] =  DB::fetch_all("SELECT id FROM chief $condition $limit", __FILE__ . ' line ' . __LINE__);
            }else{
                $data =  DB::fetch_all_array("SELECT id FROM chief $condition $limit", __FILE__ . ' line ' . __LINE__);
                $arrId = array();
                if($data !== null){
                    foreach($data as $t){
                        $arrId[]=$t['id'];
                    }
                }
                return $arrId;
            }

        }

        return $chiefs[$key];
    }

    public static function selectChiefs($cols='*', $limit='', $condition='') {
        static $chiefs;
        $key = hash('crc32b', $cols.$condition.$limit);
        if(!isset($chiefs[$key])) {
            if($condition) $condition = 'WHERE '.$condition;
            $chiefs[$key] =  DB::fetch_all("SELECT $cols FROM chief $condition $limit", __FILE__ . ' line ' . __LINE__);
        }
        return $chiefs[$key];
    }

    public static function getChiefById($id)
    {
        static $chiefs;

        if(!isset($chiefs[$id]))
        {
            $arrId = explode(',',$id);
            if(sizeof($arrId) >1){
                $chiefs[$id] = DB::select('chief', 'id IN ('.$id.')', __FILE__ . ' line ' . __LINE__);
            }else{
                $chiefs[$id] = DB::select('chief', 'id='.$id, __FILE__ . ' line ' . __LINE__);
            }

        }

        return $chiefs[$id];
    }


    public static function insert($chief) {
        //var_dump($hotel);die;
        $id = DB::insert('chief', $chief, false, __FILE__ . ' line ' . __LINE__);
        return $id;
    }

    public static function update($chief, $id)
    {

        return DB::update_id('chief', $chief, $id, __FILE__ . ' line ' . __LINE__);
    }

    public static function delBoothById($id)
    {
        return DB::update_id('chief', array("status" =>2), $id, __FILE__ . ' line ' . __LINE__);
    }


    public static function delBooths($ids)
    {
        return DB::update('chief', array("status" =>2), 'id IN (' . implode(',', $ids) . ') AND status!=2', __FILE__ . ' line ' . __LINE__);
    }



    public static function have_permit($pids, $id=false) {
        if(User::is_admin() || User::is_root()) return true;
        return false;
    }





    public static function substrhtml($str,$start,$len){

        $str_clean = substr(strip_tags($str),$start,$len);
        $pos = strrpos($str_clean, " ");
        if($pos === false) {
            $str_clean = substr(strip_tags($str),$start,$len);
        }else
            $str_clean = substr(strip_tags($str),$start,$pos);

        if(preg_match_all('/\<[^>]+>/is',$str,$matches,PREG_OFFSET_CAPTURE)){

            for($i=0;$i<count($matches[0]);$i++){

                if($matches[0][$i][1] < $len){

                    $str_clean = substr($str_clean,0,$matches[0][$i][1]) . $matches[0][$i][0] . substr($str_clean,$matches[0][$i][1]);

                }else if(preg_match('/\<[^>]+>$/is',$matches[0][$i][0])){

                    $str_clean = substr($str_clean,0,$matches[0][$i][1]) . $matches[0][$i][0] . substr($str_clean,$matches[0][$i][1]);

                    break;

                }

            }

            return $str_clean;

        }else{
            $string = substr($str,$start,$len);
            $pos = strrpos($string, " ");
            if($pos === false) {
                return substr($str,$start,$len);
            }
            return substr($str,$start,$pos);

        }

    }

}
